<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-wms__bind_head">
      <el-row :gutter="20">
        <el-col :span="6" v-for="(item, index) in showList" :key="index">
          <el-card shadow="always" class="aui-card--fill" :body-style="{ padding: '5px' }">
            <div slot="header" style="text-align: center">
              <strong>{{ item.stationDescription }}</strong>
            </div>
            <el-descriptions class="margin-top" :column="1">
              <el-descriptions-item label="砂芯图号">
                <el-tag><span v-if="item.spuNo">{{ item.spuNo }}</span>
                  <span v-else>无</span>
                </el-tag>
              </el-descriptions-item>

              <el-descriptions-item label="批次">
                <el-tag size="small"><span v-if="item.lotNo">{{ item.lotNo }}</span>
                  <span v-else>无</span>
                </el-tag>
              </el-descriptions-item>

              <el-descriptions-item label="需求数量">
                <strong><span v-if="item.amount">{{ item.amount }}</span>
                  <span v-else>0</span></strong>
              </el-descriptions-item>
            </el-descriptions>
          </el-card>
        </el-col>
      </el-row>
    </div>
    <el-divider><i class="el-icon-mobile-phone"></i></el-divider>
    <div class="mod-wms__bind">
      <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="80px">
        <el-row :gutter="20">
          <el-col :md="5" :sm="5">
            <el-form-item label="批次">
              <el-input ref="inputLotNo" v-model="dataForm.lotNo" @keyup.enter.native="getSpuByLot" placeholder="请输入批次"
                maxlength="32" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :md="7" :sm="7">
            <el-form-item label="砂芯图号" prop="spuNo">
              <el-select v-model="dataForm.spuNo" @change="pickSpuNo" placeholder="请选择上架砂芯">
                <el-option v-for="item in options" :key="item.spuId" :label="item.spuNo" :value="item">
                  <span style="float: left">{{ item.spuNo }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.lotNo }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :md="8" :sm="8">
            <el-form-item label="托盘条码" prop="contNo">
              <el-input @keyup.enter.native="bindHandle" ref="inputContNo" v-model="dataForm.contNo"
                placeholder="请输入托盘条码" maxlength="32" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :md="4" :sm="4">
            <el-form-item>
              <el-button type="primary" @click="bindHandle()">{{ $t("register") }}
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle"
        style="width: 100%">
        <el-table-column prop="contNo" label="托盘条码" header-align="center" align="center"
          min-width="220"></el-table-column>
        <el-table-column prop="bindSt" label="绑定状态" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.bindSt === 0" type="success">已登记</el-tag>
            <el-tag v-else-if="scope.row.bindSt === 1">已使用</el-tag>
            <el-tag v-else-if="scope.row.bindSt === 2">已放行</el-tag>
            <el-tag v-else type="danger">异常</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="spuNo" label="砂芯图号" header-align="center" align="center"></el-table-column>
        <el-table-column prop="lotNo" label="批次" header-align="center" align="center"></el-table-column>
        <el-table-column prop="purchaseNo" label="单据编号" header-align="center" align="center"
          min-width="145"></el-table-column>
        <el-table-column prop="stationNo" label="需求站台" header-align="center" align="center"
          min-width="100"></el-table-column>

        <el-table-column prop="creatorName" label="创建用户" header-align="center" align="center"></el-table-column>
        <el-table-column prop="createDate" label="创建日期" header-align="center" align="center" min-width="155"
          sortable></el-table-column>
      </el-table>
      <el-pagination small :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total"
        layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle" hide-on-single-page>
      </el-pagination>
    </div>
  </el-card>
</template>
<script>
import mixinViewModule from "@/mixins/view-module";
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: "/wms/bind/page",
        getDataListIsPage: true,
        deleteURL: "/wms/spuinfo",
        deleteIsBatch: false,
      },
      dataForm: {
        contNo: '',
        spuNo: '',
        lotNo: ''
      },
      showList: [],
      options: []
    };
  },
  created() {
    this.getShowInfo();
    this.getSpuInfo();
  },
  computed: {
    dataRule() {
      return {
        spuNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        lotNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //0.重置搜索框
    getReset() {
      this.dataForm = this.$options.data().dataForm;
    },
    //1.获取砂芯图号
    getSpuInfo() {
      this.$http.get(`/wms/spu-info/list`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.options = res.data;
      }).catch(() => { })
    },
    //2.选中砂芯
    pickSpuNo(value) {
      this.dataForm.spuNo = value.spuNo;
      this.dataForm.lotNo = value.lotNo;
    },
    //3.根据批号匹配砂芯
    getSpuByLot() {
      this.dataForm.spuNo = '';
      this.options.forEach(item => {
        if (item.lotNo == this.dataForm.lotNo) {
          this.dataForm.spuNo = item.spuNo;
          this.$nextTick(() => {
            this.$refs.inputContNo.focus();
            this.$refs.inputContNo.select();
          })
          return;
        }
      });
    },
    bindHandle() {
      this.$refs["dataForm"].validate((valid) => {
        if (!valid) {
          return false;
        }
        this.$http.post(`/wms/bind/delivery`, {
          "contNo": this.dataForm.contNo,
          "spuNo": this.dataForm.spuNo,
          "lotNo": this.dataForm.lotNo
        }).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: res.msg,
            type: "success",
            duration: 500,
            onClose: () => {
              this.query();
              this.$nextTick(() => {
                this.$refs.inputContNo.focus();
                this.$refs.inputContNo.select();
              })
            },
          });
        }).catch(() => { });
      })
    },
    getShowInfo() {
      this.$http.get(`/wms/vw-purchase-show/list`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.showList = res.data;
      }).catch(() => { })
    }
  },
}
</script>
<style>
.mod-wms__bind_head {
  height: 175px;
}

/* 设备卡片的卡头颜色 */
/* .el-card__header {
  background-color: yellowgreen;
} */
</style>